package com.rankeiot.boot.loader;
//import com.rankeiot.platform.PlatformApplication;





import org.noear.snack.ONode;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class BootApplication {
    private static final String LIB_PATH = "lib";
    private static final String PLUGIN_PATH = "plugin";

    public static void main(String[] args) {
        // new PlatformApplication().start(args);
       start(args);
    }

    private static void start(String[] args){
        new Thread(()->{
            try {
                URLClassLoader pluginLoader = initPluginLoader();
                Thread.currentThread().setContextClassLoader(pluginLoader);
                Class mainClass = pluginLoader.loadClass("com.rankeiot.platform.PlatformApplication");
                Consumer<String[]> mainInstance = (Consumer<String[]>) mainClass.getConstructor().newInstance();
                mainInstance.accept(args);
                synchronized (mainClass) {
                    mainClass.wait();
                }
                start(args);
            }catch (Exception e) {
                e.printStackTrace();
                System.out.println("应用加载错误");
            }
        }).start();
    }

    private static URLClassLoader initPluginLoader() throws MalformedURLException {
        List<URL> urls=new ArrayList<>();
        File libDir=new File(LIB_PATH);
        if(libDir.exists()&&libDir.isDirectory()){
            urls.addAll(getUrls(libDir));
        }
        File pluginDir = new File(PLUGIN_PATH);
        if (pluginDir.exists() && pluginDir.isDirectory()) {
            for (File dir : pluginDir.listFiles()) {
                if (dir.isDirectory()) {
                    File pluginJsonFile=new File(dir,"plugin.json");
                    if(pluginJsonFile.exists()&&pluginJsonFile.isFile()){
                        String json=read(pluginJsonFile);
                        ONode node=ONode.load(json);
                       String str= node.get("disabled").getString();
                       if("true".equals(str)){
                           continue;
                       }
                    }else{
                        continue;
                    }
                    urls.addAll(getUrls(dir));
                }
            }
        }
        return new URLClassLoader(urls.toArray(new URL[0]));
    }
    private static List<URL> getUrls(File dir) throws MalformedURLException {
        List<URL> urls=new ArrayList<>();
        for(File file:dir.listFiles()){
            if(file.isFile()&&file.getName().endsWith(".jar")){
                urls.add(file.toURI().toURL());
            }else if(file.isDirectory()){
                urls.addAll(getUrls(file));
            }
        }
        return urls;
    }
    public static String read(File file){
        byte []buff=new byte[(int)file.length()];
        FileInputStream fi=null;
        try{
            fi=new FileInputStream(file);
            fi.read(buff);
            return new String(buff,"utf-8");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fi!=null){
                try {
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
